//
//  JCSeekPageSEEKViewController.m
//  ABookOfCartoon
//
//  Created by 美国大狗 on 15-5-14.
//  Copyright (c) 2015年 Power Of Ants. All rights reserved.
//

#import "JCSeekPageSEEKViewController.h"
#import "JCModel.h"
#import "delicateOfCartoonViewController.h"

@interface JCSeekPageSEEKViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *dataArray;
@property (nonatomic, retain)NSString *sb;

@end

@implementation JCSeekPageSEEKViewController

- (void)dealloc
{
    [_StrOfSeek release];
    [_dataArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.StrOfSeek;

    [self getDataFromWeb];
}

- (void)createTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 108) style:UITableViewStylePlain];
    tableView.tag = 1000;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArray.count != 0) {
        return _dataArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse
                             ];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.textLabel.text = _dataArray[indexPath.row];
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"click!");
//    self.sb = _dataArray[indexPath.row];
//    [self getDataFromWeb23333];
    delicateOfCartoonViewController *jump = [[delicateOfCartoonViewController alloc] init];
    jump.sb = _dataArray[indexPath.row];
    [self.navigationController pushViewController:jump animated:YES];
}


- (void)getDataFromWeb
{
    //网址 -> string
    NSString *strA = [NSString stringWithFormat:@"http://112.124.96.190:9090/manhuadaquan4appjobs/ComicHandle.ashx?method=getkey&key=%@", self.StrOfSeek];
    [self.StrOfSeek release];
    //有中文?转码!
    NSString *str = [strA stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //string -> url
    NSURL *url = [NSURL URLWithString:str];
    //url -> request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //建立连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.dataArray = [[NSMutableArray array] init];
        NSArray *arr = result;
        if (arr.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OTZ" message:@"啊哦,没有找到主人说的漫画了啦...." delegate:self cancelButtonTitle:@"我再输的仔细点.." otherButtonTitles: nil];
            [alert show];
            [alert release];
            [self.navigationController popViewControllerAnimated:YES];
        } else if(arr.count != 0){
            for (NSString *strB in arr) {
//                JCModel *m = [[JCModel alloc] initWithDic:dic];
                [_dataArray addObject:strB];
//                NSLog(@"%@", _dataArray);
//                NSLog(@"------");
            }
            NSLog(@"%@",str);
            [(UITableView *)[self.view viewWithTag:1000] reloadData];

        }
        
    }];
    [self createTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
